package project.messagestack;

import java.io.InputStream;
import java.util.StringTokenizer;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;

public class MessageView extends Activity {
    Context mContext = this;
	  public static Bitmap loadContactPhoto(ContentResolver cr, Resources res, long  id) {
		    Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
		    InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
		    if (input == null) {
		        return BitmapFactory.decodeResource(res, R.drawable.ic_contact_picture);
		    }
		    return BitmapFactory.decodeStream(input);
		}
	
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.message_view);
    
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    //ImageView mv = (ImageView)findViewById(R.id.MVbg);
    RelativeLayout rl = (RelativeLayout)findViewById(R.id.relativeMV);
    rl.setWillNotDraw(false);
	if(preferences.getString("colorSettings", "null").equalsIgnoreCase("Dark Color")){
		rl.setBackgroundResource(R.drawable.bg_normal);
	}
	else if(preferences.getString("colorSettings", "null").equalsIgnoreCase("Bright Color")){
		rl.setBackgroundResource(R.drawable.bg_normal_br);
	}
	else if(preferences.getString("colorSettings", "null").equalsIgnoreCase("Blue Color")){
		rl.setBackgroundResource(R.drawable.bg_normal_bl);
	}
	else if(preferences.getString("colorSettings", "null").equalsIgnoreCase("Green Color")){
		rl.setBackgroundResource(R.drawable.bg_normal_gr);
	}
	else if(preferences.getString("colorSettings", "null").equalsIgnoreCase("Red Color")){
		rl.setBackgroundResource(R.drawable.bg_normal_re);
	}
	rl.invalidate();

    if (getIntent() != null) {
      
      Intent i = getIntent();      
      ((TextView)findViewById(R.id.userName)).setText(i.getStringExtra("user"));
      ((TextView)findViewById(R.id.messageContent)).setText(i.getStringExtra("content"));
      ((TextView)findViewById(R.id.messageService)).setText(i.getStringExtra("service"));
      if((i.getStringExtra("service")).compareToIgnoreCase("buzz")== 0){
    	  ((ImageView)findViewById(R.id.srvIcon)).setImageResource(R.drawable.icon_buzz);
      }
      else if((i.getStringExtra("service")).compareToIgnoreCase("facebook")== 0){
    	  ((ImageView)findViewById(R.id.srvIcon)).setImageResource(R.drawable.icon_fb);
      }
      else if((i.getStringExtra("service")).compareToIgnoreCase("sms")== 0){
    	  ((ImageView)findViewById(R.id.srvIcon)).setImageResource(R.drawable.icon_sms);
      }
      else if((i.getStringExtra("service")).compareToIgnoreCase("nk")== 0){
    	  ((ImageView)findViewById(R.id.srvIcon)).setImageResource(R.drawable.icon_nk);
      }
      else if((i.getStringExtra("service")).compareToIgnoreCase("twitter")== 0){
    	  ((ImageView)findViewById(R.id.srvIcon)).setImageResource(R.drawable.icon_twitter);
      }
      else{
    	  ((ImageView)findViewById(R.id.srvIcon)).setImageResource(R.drawable.icon_mail);
      }

      long id = -1;
      if ((i.getStringExtra("service")).compareToIgnoreCase("SMS")==0) {
        String phoneNumber = i.getStringExtra("phone");
      	Cursor c =mContext.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
      	                                                         Uri.encode(phoneNumber)),
      	                                    new String[] { PhoneLookup._ID, PhoneLookup.DISPLAY_NAME},
      	                                    null,
      	                                    null,
      	                                    null);
      	if (c.moveToFirst()) {
          id = c.getLong(c.getColumnIndexOrThrow(PhoneLookup._ID));
          String s = c.getString(c.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
          StringTokenizer tokens = new StringTokenizer(s," ");
          ((TextView)findViewById(R.id.userName)).setText(tokens.nextToken());
          ((TextView)findViewById(R.id.userName2)).setText(tokens.nextToken());
          Bitmap bmp = loadContactPhoto(mContext.getContentResolver(), mContext.getResources(), id);
          bmp = Bitmap.createScaledBitmap(bmp, 100, 100, false);
          ((ImageView)findViewById(R.id.userPhoto)).setImageBitmap(bmp);
      	} else {
      		((ImageView)findViewById(R.id.userPhoto)).setImageResource(R.drawable.ic_contact_picture);
      	}
      } else {
    	  ((ImageView)findViewById(R.id.userPhoto)).setImageResource(R.drawable.ic_contact_picture);
      }
    }
  }
  
}
